﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Drawing;
using System.Data;

namespace mfe.bll
{
    public partial class Photo
    {
        /*
        public static List<Photo> PhotoCollection(Enums.InfoFormat format, string itemGuidID)
        {
            List<Photo> retval = new List<Photo>();

            try
            {
                mfe.lib.data.Database db = mfe.lib.data.DatabaseFactory.CreateDatabase();
                string sql;
                if (format == Enums.InfoFormat.Simple)
                    sql = "select ID, PhotoGuidID, ItemGuidID, Title, Thumbnail from Photo where ItemGuidID=@ItemGuidID";
                else
                    sql = "select * from Photo where ItemGuidID=@ItemGuidID";
                DbCommand cmd = db.GetSqlStringCommand(sql);
                db.AddInParameter(cmd, "ItemGuidID", DbType.String, itemGuidID);
                IDataReader reader = db.ExecuteReader(cmd);
                while (reader.Read())
                {
                    Photo p = new Photo();
                    p._ID = int.Parse(reader["ID"].ToString());
                    p._PhotoGuidID = reader["PhotoGuidID"].ToString();
                    p._ItemGuidID = reader["ItemGuidID"].ToString();
                    p._Title = reader["Title"].ToString();
                    p._Thumbnail = (byte[])reader["Thumbnail"];
                    if (format != Enums.InfoFormat.Simple)
                    {
                        p._Data = (byte[])reader["Data"];
                    }
                    retval.Add(p);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                mfe.lib.Logger.Log(ex);
            }
            return retval;
        }

        public static List<Image> PhotoCollection()
        {
            List<Image> retval = new List<Image>();

            try
            {
                mfe.lib.data.Database db = mfe.lib.data.DatabaseFactory.CreateDatabase();
                DbCommand cmd = db.GetSqlStringCommand("select * from Photo");
                IDataReader reader = db.ExecuteReader(cmd);
                while (reader.Read())
                {
                    byte[] content = (byte[])reader["Thumbnail"];
                    MemoryStream ms = new MemoryStream(content);
                    retval.Add(Image.FromStream(ms));
                    ms.Close();
                    ms.Dispose();
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                mfe.lib.Logger.Log(ex);
            }
            return retval;
        }
        */
        
        public static bool UpdatePhotoTitle(string photoGuidID, string newtitle)
        {
            ///TODO: can this function be eliminated and use the Save() instead?
            if (photoGuidID == null || photoGuidID == string.Empty) return false;

            try
            {
                mfe.lib.data.Database db = mfe.lib.data.DatabaseFactory.CreateDatabase();
                DbCommand cmd = db.GetSqlStringCommand("update Photo set Title=@Title where PhotoGuidID=@PhotoGuidID");
                db.AddInParameter(cmd, "Title", System.Data.DbType.String, newtitle);
                db.AddInParameter(cmd, "PhotoGuidID", System.Data.DbType.String, photoGuidID);
                db.ExecuteNonQuery(cmd);

                return true;
            }
            catch (Exception ex)
            {
                mfe.lib.Logger.Log(ex);
                return false;
            }
        }
    }
}
